<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>盒子水平居中</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .box {
            width: 400px;
            height: 400px;
            border: 2px solid #000;
            background-color: aqua;
            margin: 10px auto;
        }
        .box2 {
            position: absolute;
            left: 50%;
            width: 300px;
            height: 300px;
            margin-left: -150px;
            border: 2px solid #000;
            background-color: blueviolet;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <div class="box2"></div>
</body>
</html>